#coding = UTF-8

import hashlib
from settings import DEFAULT_AUTH_SALT
from core.error import UserError

class AuthPassword:
    def __init__(self, password):
        self.password = self._gen_pwd(password)

    @staticmethod
    def _gen_pwd(password):
        m = hashlib.sha1()
        m.update(password + DEFAULT_AUTH_SALT)
        return m.hexdigest()

    def verify(self, password):
        return self.password == password



def name_pwd_validator(login_name, password):
    # length validate
    name_len = len(login_name)

    if name_len < 6 and name_len > 16:
        raise UserError('user name length is not right. (6~18)')

    pwd_len = len(password)

    if pwd_len < 6 and pwd_len > 16:
        raise UserError('password length is not right. (6~16)')
